/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leulounge.model;

import com.google.gson.Gson;
import com.leulounge.util.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Andres Godoy
 */
public class Inventory {
    
    private List<Product> inventory;

    public Inventory(){
        this.inventory = new ArrayList<>();
    }
    /**
     * @return the inventory
     */
    public List<Product> getInventory() {
        return inventory;
    }

    /**
     * @param inventory the inventory to set
     */
    public void setInventory(List<Product> inventory) {
        this.inventory = inventory;
    }
    
    public void addToInventory(Product product){
        if (inventory.get(inventory.indexOf(product)) == null){
            try {
                inventory.add(product);
                Connection connection = Connection.getInstance();
                Gson gson = new Gson();
                connection.save("inventory", gson.toJson(this));
            } catch (Exception ex) {
                Logger.getLogger(Inventory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public void deleteFromInventory(Product product){
        if (inventory.get(inventory.indexOf(product)) != null){
            try {
                inventory.remove(product);
                Connection connection = Connection.getInstance();
                Gson gson = new Gson();
                connection.save("inventory", gson.toJson(this));
            } catch (Exception ex) {
                Logger.getLogger(Inventory.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
